<template>
	<el-breadcrumb separator="/">
		<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
		<el-breadcrumb-item>文章分类管理</el-breadcrumb-item>
	</el-breadcrumb>
	<!-- 搜索框 -->
	<el-form :inline="true" :model="queryInfo" class="demo-form-inline">
		<el-form-item label="分类名">
			<el-input v-model="queryInfo.classifyName" placeholder="请输入分类名" clearable @clear="queryDataList" />
		</el-form-item>
		<el-form-item>
			<el-button type="primary" size="large" @click="queryDataList">搜索</el-button>
			<el-button type="primary" size="large" @click="add">新增</el-button>
			<el-button type="danger" size="large" @click="batchremove">批量删除</el-button>
		</el-form-item>
	</el-form>

	<!-- 表格 -->
	<el-table :data="dataList" border style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
		<el-table-column type="selection" width="55" />
		<el-table-column prop="id" label="ID" width="300" align="center"></el-table-column>
		<el-table-column prop="classifyName" label="分类名" align="center"></el-table-column>
		<<el-table-column label="状态" align="center">
			<template #default="scope">
				<el-tag :type="scope.row.del === 0 ? 'success' : 'danger'">
					{{ statusMap[scope.row.del] }}
				</el-tag>
			</template>
			</el-table-column>
			<el-table-column label="操作" width="300" align="center">
				<template #default="scope">
					<el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="remove(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
	</el-table>

	<div class="demo-pagination-block">
		<div class="demonstration"></div>
		<el-pagination v-model:current-page="queryInfo.pageNum" v-model:page-size="queryInfo.pageSize"
			:page-sizes="[5, 10, 15, 20]" :size="defaults" layout="total, sizes, prev, pager, next, jumper"
			:total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
	</div>

	<el-dialog :title="title" v-model="dialogFormVisible" width="800" @close="cancle">
		<el-form :model="articleClassifyData" label-width="100">
			<el-form-item label="分类名" label-position="top">
				<el-input v-model="articleClassifyData.classifyName" autocomplete="off" placeholder="请输入分类名"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">
					确 定
				</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script setup>
	import {
		reactive,
		ref,
		getCurrentInstance,
		onMounted
	} from 'vue';

	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus';

	import {
		Plus
	} from '@element-plus/icons-vue'

	const total = ref(0)

	const queryInfo = reactive({
		id: '',
		classifyName: '',
		del: '',
		pageNum: 1,
		pageSize: 5
	})

	const statusMap = {
		0: '正常',
		1: '异常',
	};

	const dataList = ref([])
	const axios = getCurrentInstance().appContext.config.globalProperties.axios;

	const queryDataList = () => {
		axios.get('articleClassify/page', {
			params: queryInfo
		}).then((resp) => {
			if (resp.code == 200) {
				console.log('文章分类查询成功');
				dataList.value = resp.data.list;
				total.value = resp.data.total;
			} else {
				ElMessage.error('查询失败')
			}
		})
	}

	const title = ref()
	const dialogFormVisible = ref(false)

	const articleClassifyData = ref({
		id: '',
		classifyName: '',
		del: ''
	})

	onMounted(() => {
		queryDataList()
	})

	const handleSizeChange = (val) => {
		queryDataList()
	}

	const handleCurrentChange = (val) => {
		queryDataList()
	}

	const add = () => {
		title.value = '新增'
		dialogFormVisible.value = true
	}

	const cancle = () => {
		console.log('你点击了取消按钮');
		articleClassifyData.value = {
			classifyName: '',
		}
		dialogFormVisible.value = false
	}

	const confirm = () => {
		if (articleClassifyData.value.id) {
			console.log('你点击了确定按钮');
			axios.put('articleClassify', articleClassifyData.value)
				.then((resp) => {
					if (resp.code == 200) {
						ElMessage({
							message: '修改成功',
							type: 'success',
						})
					} else {
						ElMessage.error('修改失败')
					}
					queryDataList()
					dialogFormVisible.value = false
				})
		} else {
			console.log('你点击了确定按钮');
			axios.post('articleClassify', articleClassifyData.value)
				.then((resp) => {
					if (resp.code == 200) {
						ElMessage({
							message: '新增成功',
							type: 'success',
						})
					} else {
						ElMessage.error('新增失败')
					}
					queryDataList()
					dialogFormVisible.value = false
				})
		}
	}

	const edit = (row) => {
		console.log(row);
		title.value = '编辑'
		dialogFormVisible.value = true

		row.classifyName = row.classifyName + ''
		articleClassifyData.value = row
	}

	const remove = (id) => {
		console.log(id);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'提示', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			).then(() => {
				axios.delete(`articleClassify/${id}`)
					.then((resp) => {
						if (resp.code == 200) {
							ElMessage.success('删除成功')
						} else {
							ElMessage.error('删除失败')
						}
						queryDataList()
					})
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '已取消删除',
				})
			})
	}

	const multipleSelection = ref([]);

	const handleSelectionChange = (val) => {
		multipleSelection.value = val.map(item => {
			return item.id
		})
		console.log(multipleSelection.value);
	}

	const batchremove = () => {
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'提示', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			).then(() => {
				axios.delete(`articleClassify`, {
						params: {
							ids: multipleSelection.value.join(',') // 拼接为逗号分隔的字符串（如 "1,2,3"）
						}
					})
					.then((resp) => {
						if (resp.code == 200) {
							ElMessage.success('删除成功')
						} else {
							ElMessage.error('删除失败')
						}
						queryDataList()
					})
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '已取消删除',
				})
			})
	}
</script>

<style scoped>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.demo-form-inline .el-input {
		--el-input-width: 220px;
	}

	.demo-form-inline .el-select {
		--el-select-width: 220px;
	}

	.demo-pagination-block .demonstration {
		margin-bottom: 16px;
	}

	.el-table {
		line-height: 40px;
	}
</style>